/*
* Copyright (c) Huawei Technologies Co., Ltd. 2012-2018. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Created by wang-bain on 2021/11/11.


#include "acl/acl.h"
#include "driver/ascend_hal.h"
#include "driver/dsmi_common_interface.h"

#include <algorithm>
#include <dirent.h>
#include <fstream>
#include <getopt.h>
#include <map>
#include <cmath>
#include <memory.h>
#include <numeric>
#include <string>
#include <sys/time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <vector>

using namespace std;

#ifndef XACL_FMK_UTILS_H
#define XACL_FMK_UTILS_H

// 返回值
typedef enum Result {
    SUCCESS = 0,
    FAILED = 1
} Result;

// 模型信息, 包含:
// 模型ID, 模型分档信息, 模型上下文, 模型输入, 输入文件列表, 输入Shape, 输入大小, 拼接输入个数, 模型输入描述, 模型描述, 模型输入输出等
struct ModelInfo {
    uint32_t modelId = 0;
    uint32_t batchRank = 0;
    uint32_t heightRank = 0;
    uint32_t widthRank = 0;
    aclmdlIODims dimsRank;
    vector<aclrtContext> v_context;
    vector<string> v_inputItems;
    vector<vector<string>> v_allInputFiles;
    vector<vector<int64_t>> v_inputShape;
    vector<uint32_t> v_inputSize;
    vector<size_t> v_filesPerBatch;
    vector<aclTensorDesc *> v_inputDesc;
    vector<double> v_totalTime;
    aclmdlDesc *modelDesc = nullptr;
    aclmdlDataset *input = nullptr;
    aclmdlDataset *output = nullptr;
};

void Split(const string &tokens, vector<string> &v_tokens, const string &delimiters);

Result InitAndCheckParams(int argc, char *argv[], map<char, string> &params);

Result InitResource(struct ModelInfo &modelInfo, map<char, string> &params);

Result LoadModelFromFile(struct ModelInfo &modelInfo, map<char, string> &params);

Result CreateModelDesc(struct ModelInfo &modelInfo);

Result CalculateInputSize(struct ModelInfo &modelInfo, map<char, string> &params);

Result CreateInput(struct ModelInfo &modelInfo, map<char, string> &params, uint32_t fileIdx);

Result CreateInputDesc(struct ModelInfo &modelInfo, map<char, string> &params);

Result CreateOutput(struct ModelInfo &modelInfo, map<char, string> &params);

Result Execute(struct ModelInfo &modelInfo, map<char, string> &params, uint32_t fileIdx);

Result WriteResult(struct ModelInfo &modelInfo, map<char, string> &params);

Result DestroyResource(struct ModelInfo &modelInfo, map<char, string> &params);

#endif // XACL_FMK_UTILS_H
